# -*- coding:utf-8 -*-
# title           :
# description     :
# author          :Python超人
# date            :2023-10-31
# link            :https://gitcode.net/pythoncr/
# python_version  :3.8
# ==============================================================================
import sxtwl
from calendar_style import CalendarStyle
import calendar

# 如果本地没有安装工具包，请执行
# pip install -i https://mirrors.aliyun.com/pypi/simple/ xlwt sxtwl

# 天干
TIAN_GAN = ["甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"]
# 地支
DI_ZHI = ["子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"]

# 六十甲子表
GZ = [''] * 60
for i in range(60):
    GZ[i] = TIAN_GAN[i % 10] + DI_ZHI[i % 12]

# 生肖
CN_ZODIAC = ['鼠', '牛', '虎', '兔', '龙', '蛇', '马', '羊', '猴', '鸡', '狗', '猪']

# 二十四节气
JIE_QI = ["冬至", "小寒", "大寒", "立春", "雨水", "惊蛰", "春分", "清明", "谷雨", "立夏", "小满", "芒种",
          "夏至", "小暑", "大暑", "立秋", "处暑", "白露", "秋分", "寒露", "霜降", "立冬", "小雪", "大雪"]

# 农历月份
YUE_FEN = ["正月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "冬月", "腊月"]
# 农历日期
NLRQ = ["初一", "初二", "初三", "初四", "初五", "初六", "初七", "初八", "初九", "初十",
        "十一", "十二", "十三", "十四", "十五", "十六", "十七", "十八", "十九", "二十",
        "廿一", "廿二", "廿三", "廿四", "廿五", "廿六", "廿七", "廿八", "廿九", "三十"]
# 添加节日：节日表[[月，日, 节日] * n]，GL_FESTIVAL 公历节日，NL_FESTIVAL 农历节日
GL_FESTIVAL = [[1, 1, "元旦"], [3, 8, "妇女节"], [5, 1, "劳动节"], [5, 4, "青年节"], [6, 1, "儿童节"],
               [7, 1, "建党节"], [8, 1, "建军节"], [9, 10, "教师节"], [10, 1, "国庆"]]
NL_FESTIVAL = [[12, 30, '除夕'], [1, 1, "春节"], [1, 15, "元宵节"], [5, 5, "端午节"],
               [7, 7, "七夕"], [7, 15, "中元节"], [8, 15, "中秋节"], [9, 9, '重阳节'],
               [12, 8, '腊八节']]
# 中文周
CN_WEEKS = ['一', '二', '三', '四', '五', '六', '日']


def get_ganz_zodiac(year):
    """
    获取年份的天干地支和生肖
    @param year:
    @return: (当前年份, 天干地支, 生肖)
    """
    lunar_day = sxtwl.fromSolar(year, 6, 1)
    y_tg = lunar_day.getYearGZ(True)
    gan_zhi = TIAN_GAN[y_tg.tg] + DI_ZHI[y_tg.dz]
    sheng_xiao = CN_ZODIAC[y_tg.dz]
    return year, gan_zhi, sheng_xiao


def get_calendar_info_from_solar(year: int, month: int, day: int):
    """
    根据公历年月日获取日历的信息，（农历、二十四节气、农历节假日和公历节假日）
    :param year: 公历年份，int类型
    :param month: 公历月份，int类型，取值范围为 1-12
    :param day: 公历日期，int类型，取值范围为 1-31
    :return: 返回三个日历信息 (a, b, c)
        a: 返回的日历名称。包含农历的中文名称、二十四节气名称、农历节假日名称、公历节假日名称
        b: 返回的名称类型。0.农历的中文名称 1.二十四节气名称 2.农历节假日名称 3.公历节假日名称
        c: 返回农历年月日。 (lunar_year, lunar_month, lunar_day)
    """
    lunar = sxtwl.fromSolar(year, month, day)
    lunar_year = lunar.getLunarYear()
    lunar_month = lunar.getLunarMonth()
    lunar_day = lunar.getLunarDay()

    # 公历节日
    for f in GL_FESTIVAL:
        if f[0] == month and f[1] == day:
            return f[2], 3, (lunar_year, lunar_month, lunar_day)

    # 农历节日
    for f in NL_FESTIVAL:
        if f[0] == lunar_month and f[1] == lunar_day:
            return f[2], 2, (lunar_year, lunar_month, lunar_day)

    # 二十四节气名称
    if lunar.hasJieQi():
        return JIE_QI[lunar.getJieQi()], 1, (lunar_year, lunar_month, lunar_day)

    # 农历名称
    if lunar_day == 1:
        return YUE_FEN[lunar_month - 1], 0, (lunar_year, lunar_month, lunar_day)
    else:
        return NLRQ[lunar_day - 1], 0, (lunar_year, lunar_month, lunar_day)


def get_calendar_list(year, month):
    """
    获取日历列表（返回日历列表包含公历和农历信息）
    @param year: 年份
    @param month: 月份
    @return: 返回日历列表包含公历和农历信息 solar_list, lunar_list
    """
    cal = calendar.TextCalendar()

    # 获取一个月的日历数据 formatmonth
    calendar_str = cal.formatmonth(theyear=year, themonth=month)

    # 2024 => 2024年、周日 => 日
    calendar_str = calendar_str.replace(str(year), f"{year}年").replace("周", "")
    cal_list = calendar_str.split("\n")  # 用“\n 换行符”进行分割

    # 存放所有的公历数据
    solar_list = []
    # 存放所有的农历数据
    lunar_list = []

    for i in range(2, 7):  # 第3行开始到7行是公历日期
        row = cal_list[i]
        lunar_infos = []  # 存放农历、节假日的信息
        solar_infos = []  # 存放公历信息
        # 循环： row[0:3] -> row[18:20]
        for j in range(0, 21, 3):
            d = row[j: j + 3]  # row[0:3] ... row[18:20]
            solar_infos.append(d)
            if d.strip() == '':  # d 为空说明没有日期，为了对齐，农历也追加一个空
                lunar_infos.append('')
                continue
            day = int(d)
            # 根据  cal_info = (a, b, c)
            #         a: 返回的日历名称。包含农历的中文名称、二十四节气名称、农历节假日名称、公历节假日名称
            #         b: 返回的名称类型。0.农历的中文名称 1.二十四节气名称 2.农历节假日名称 3.公历节假日名称
            #         c: 返回农历年月日。
            cal_info = get_calendar_info_from_solar(2024, 2, day)
            # （农历、节假日名称）
            lunar_infos.append(cal_info)
        # 将公历、农历的日历数据放入列表，作为后面代码使用
        solar_list.append(solar_infos)
        lunar_list.append(lunar_infos)

    return solar_list, lunar_list


def create_style_01():
    """
    创建日历样式1
    @return:
    """
    return CalendarStyle(font_color=0, bg_color=26, highlight_font_color=2,
                         border_style=1, border_color=0, header_bottom_border_style=0,
                         header_font_color=1, header_bg_color=52,
                         header_highlight_font_color=1, header_highlight_bg_color=52)


def create_style_02():
    """
    创建日历样式2
    @return:
    """
    return CalendarStyle(font_color=0, bg_color=1, highlight_font_color=2,
                         border_style=1, border_color=0, header_bottom_border_style=0,
                         header_font_color=1, header_bg_color=0,
                         header_highlight_font_color=1, header_highlight_bg_color=0)
